import $ from './jquery.min.js';
$(() => {
    // 新增导演
    $("#addClassesBtn").on('click', function () {
        $.ajax({
            url: '/Directors',
            data: {
                directorname: $("#addClassesName").val()
            },
            type: 'post',
            success({ message, status }) {
                if (status == 200) {
                    alert(message);
                } else {
                    alert(message);
                }
            }
        })
    })
    // 导演列表
    $('dd .directorsList').click(function () {
        $.ajax({
            url: '/Directors/',
            success({ message, status, directors }) {
                directorRender(directors);
            }
        })
    })

    $("#directorTb").on('click', 'a', function () {
        const directorname = $(this).parents('tr').find('.directorname').text();
        const result = confirm(`你确定要删除 ${directorname} 吗？`);
        if (result) {
            const directorId = $(this).attr("data-directorId")
            $.ajax({
                url: `/Directors/${directorId}`,
                type: 'delete',
                success({ message, status, directors }) {
                    alert("删除成功");
                    directorRender(directors);
                }
            })
        }
    })
})

function directorRender(data) {
    $("#directorTb").html('');
    let result = data.map(director =>
        `<tr>
            <td>${director._id}</td>
            <td class="directorname">${director.directorname}</td>
            <td>
                <a href="javascript:;" data-directorId="${director._id}">删除</a>
            </td>
        </tr>`
    );
    $("#directorTb").html(result);
}